package tests;

import java.lang.System;
import java.nio.ByteBuffer;

import global.Convert;
import java.io.*;
import java.util.Arrays;
import java.util.List;

import diskmgr.PCounter;
import diskmgr.Stats;
import diskmgr.Stream;

import tripleheap.Triple;
import tripleheap.TripleHeapfile;
import labelheap.LabelHeapFile;
import global.*;

class Records {
	public String name;
	private byte[] data;

	public Records() {
	}

	public Records(int _reclen) {
		setRecLen(_reclen);
		data = new byte[_reclen];
	}

	public void setRecLen(int size) {
	}

	public byte[] toByteArray() throws java.io.IOException {
		//		Convert.setStrValue(name, 0, data);
		return data;
	}
}

public class batchinsert {

	public boolean insert(String subject, String predicate, String object,double confidence) throws IOException
	{
		EID eid=new EID();
		//LabelHeapFile lf=SystemDefs.entityLabelHeap;
		Records rec = new Records(subject.length());
		rec.name = subject;
		try {
			eid = SystemDefs.JavabaseDB.insertEntity(rec.name);
			//sub = new EID(lid);
			//
		} catch (Exception e) {
			System.out.println("Error inserting record \n");
			e.printStackTrace();
		}

		PID pid=new PID();
		try{
			pid=SystemDefs.JavabaseDB.insertPredicate(predicate);
		}
		catch(Exception e){
			e.printStackTrace();
		}

		EID oid=new EID();
		//LabelHeapFile lf=SystemDefs.entityLabelHeap;
		try {
			oid = SystemDefs.JavabaseDB.insertEntity(object);
			//sub = new EID(lid);
			//
		} catch (Exception e) {
			System.out.println("Error inserting record \n");
			e.printStackTrace();
		}

		byte [] combined = new byte[32];

		Convert.setIntValue(eid.pageNo.pid, 0, combined);
		Convert.setIntValue(eid.slotNo, 4, combined);

		Convert.setIntValue(pid.pageNo.pid, 8, combined);
		Convert.setIntValue(pid.slotNo, 12, combined);

		Convert.setIntValue(oid.pageNo.pid, 16, combined);
		Convert.setIntValue(oid.slotNo, 20, combined);
		Convert.setDblValue(confidence, 24, combined);

		TID tid = new TID();
		try {
			double co=Convert.getDblValue(24, combined);
			tid = SystemDefs.JavabaseDB.insertTriple(combined);
			//sub = new EID(lid);
			//
		} catch (Exception e) {
			System.out.println("Error inserting triple \n");
			e.printStackTrace();
		}

		return true;
	}

	public static void main(String[] args) throws IOException {
		int i = 0;
		System.out.println("\n Batchinserting...\n");
		String Datafilename = args[0];
		int indexoption = Integer.parseInt(args[1]);
		
		File file = new File(args[2]);
		boolean exists = file.exists();
		if (exists) {
			SystemDefs.MINIBASE_RESTART_FLAG=false;
			SystemDefs sysdef = new SystemDefs(args[2], 0, 5500, "Clock");
		} else {
			SystemDefs sysdef = new SystemDefs(args[2], 5000, 5500, "Clock");
		}
		batchinsert b = new batchinsert();
		try {
			FileInputStream fstream = new FileInputStream(Datafilename);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				String delims = "[:\t]+";
				String str2 = strLine.trim();
				String[] tokens = str2.split(delims);

				String subject = tokens[1].trim();
				String predicate = tokens[2].trim();
				String object = tokens[3].trim();
				String conf = tokens[4].trim();

			if (b.insert(subject, predicate, object, Double.parseDouble(conf))) {
				i++;
				System.out.println("success !!" + i);
			}
			
			}
			in.close();
			SystemDefs.JavabaseDB.closeDB();
			System.out.println("No of reads : "+PCounter.rcounter+"\nNo of writes : "+PCounter.wcounter);
			System.out.println("\nNumber of Entities inserted: "+Stats.encounter+"\nNumber of Predicates inserted: "+Stats.prcounter+"\nNumber of Triples inserted: "+Stats.tcounter);
			//Stream s1=SystemDefs.JavabaseDB.openStream("db1", 0, "0", "knows","0", 0, 10);
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}


}
